const Actions = require('./actions');
import { Control } from 'react-keeper'
const DB = require('app/db.js');
import { monStorage, Storage } from 'clientConfig/util/StoreData';
import { NEW, RUNNING, TERMINATED, COMPLETED, CANCELED } from 'clientConfig/constants'
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        
    },

    onFetch: function (pageNum, props, success, error) {
        let t = this;
        if(Control.state.origin_title=="质量管理"||Control.state.origin_title=="进度跟踪"||Control.state.origin_title=="版本管理" ){
            DB.Approval.get_approval_list({
                module_type:"workplan",
                PageNum: pageNum,
                CountForAPage: props.pageSize,
                status:COMPLETED,
                proj_category:"software",
                proj_name: props.proj_name ? props.proj_name : "" ,
                proj_num: props.proj_num ? props.proj_num : "",
                id:Storage.get('user.id') 
            })
            .then(function (content) {
                var list = [];
                var obj = {}
                content.list.map((item,index)=>{
                    obj = {
                        "badge": false,
                        "title": item.proj_name,
                        "subtitle":!window.isBp?"":"合同签订",//window.isBp?"":"合同签订"
                        "process_id":item.process_id,
                        "folder_id":item.folder_id,
                        "content": [
                            {"unitcontent": item.proj_num}
                        ]
                    }
                    list.push(obj);
                    obj = {};
                });
                if(!window.isBp|| Control.state.origin_title=="进度跟踪"|| Control.state.origin_title=="质量管理"){
                    const hasNoMore = list.length <  props.pageSize;
                    t.data.listData = list;
                    if(props.proj_name || props.proj_num)
                        t.updateComponent()
                    if(success)
                        success(hasNoMore, list);
                }else{
                    return list
                }
                // const hasNoMore = list.length <  props.pageSize;
                // t.data.listData = list;
                // if(props.proj_name || props.proj_num)
                //     t.updateComponent()
                // if(success)
                //     success(hasNoMore, list);
            }).then(function (list){
                if(!list){
                    return;
                }
                DB.Approval.get_approval_list({
                    module_type:"tender",
                    PageNum: pageNum,
                    CountForAPage: props.pageSize,
                    status:COMPLETED,
                    proj_category:"tenderplan",
                    proj_name: props.proj_name ? props.proj_name : "" ,
                    proj_num: props.proj_num ? props.proj_num : "",
                    id:Storage.get('user.id') 
                })
                .then(function (content) {
                    var alllist = list;
                    var obj = {}
                    content.list.map((item,index)=>{
                        obj = {
                            "badge": false,
                            "title": item.proj_name,
                            "subtitle":"招标",
                            "process_id":item.process_id,
                            "folder_id":item.folder_id,
                            "content": [
                                {"unitcontent": item.proj_num}
                            ]
                        }
                        alllist.push(obj);
                        obj = {};
                    });
                const hasNoMore = alllist.length < 2 * props.pageSize;
                t.data.listData = alllist;
                if(props.proj_name || props.proj_num)
                    t.updateComponent()
                if(success)
                    success(hasNoMore, alllist);
                })
                
            }).catch(function (error) {

            });
        }
    },

    updateComponent: function() {
        this.trigger(this.data);
    },

    getInitialState: function() {
        return this.data;
    }
});
